<template>
  <div id="user_id">
    <div class="user_top">
      <div class="user_add">
        <el-button size="mini" round @click="isShow.flag = true"
          >增加</el-button
        >
      </div>
    </div>
    <el-table
      :data="user_list"
      style="width: 100%; margin-bottom: 20px; margin-top: 20px"
    >
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="rolename" label="所属角色"></el-table-column>
      <el-table-column prop="status" label="账号状态" v-slot="$props">
        <template>
          <el-button
            v-if="$props.row.status === 1"
            size="mini"
            type="success"
            plain
            round
            >正常</el-button
          >
          <el-button
            v-else-if="$props.row.status === 0"
            size="mini"
            type="danger"
            plain
            round
            >禁用</el-button
          >
        </template>
      </el-table-column>

      <el-table-column label="操作" v-slot="$props">
        <template>
          <el-button       
            size="mini"
            round
            @click="updateUser($props.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            round
            @click="delUser($props.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="page_size"
      prev-text="上一页"
      next-text="下一页"
      @current-change="change_page"
    >
    </el-pagination>
    <addUser
      ref="add_user_ref"
      :isShow="isShow"
      @reloadUser="reloadUser"
    ></addUser>
  </div>
</template>

<script>
import addUser from "../components/addUser.vue";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      isShow: { flag: false },
      page: 1,
      page_size: 5,
      total: 0,
    };
  },
  created() {
    this.UserActions({ page: this.page, size: this.page_size });
    this.UserCountAction();
  },
  methods: {
    Del(id) {
      this.$http.post("/api/userdelete", { id }).then((res) => {
        if (res.code === 200) {
          this.UserActions({ page: this.page, size: this.page_size });
          this.UserCountAction();
        }
      });
    },
    delUser(id) {
      this.$confirm("确定要删除用户吗？", "删除用户", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          this.Del(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    updateUser(obj) {
      this.isShow.flag = true;
      // obj.password = "123";
      this.$refs.add_user_ref.setRuleForm(obj);
    },
    reloadUser() {
      this.UserActions({ page: this.page, size: this.page_size });
      this.UserCountAction();
    },
    change_page(page) {
      this.page = page;
      this.UserActions({ page: this.page, size: this.page_size });
    },
    ...mapActions("UserStore", ["UserActions", "UserCountAction"]),
  },
  computed: {
    ...mapState({
      user_list: (state) => state.UserStore.user_list,
      count: (state) => state.UserStore.count,
    }),
  },
  components: {
    addUser,
  },
};
</script>

<style>
#user_id .user_top {
  display: flex;
  justify-content: space-between;
  flex-direction: row-reverse;
}
#user_id .el-table {
  margin-top: 20px;
}
#user_id .user_add > button {
  width: 80px;
  color: #ff5d3e;
  background-color: white;
}
</style>